package com.n9.buymsg.service.qqonline.converter;

import com.n9.sdk.model.PurchaseMessage;

import java.util.Objects;

/**
 * 消息内容过滤器
 * @Author: ice
 * @Date: 2020-07-06 9:09
 */
public class MsgBodyFilter implements PurchaseMessageFilter {
    private String[] keywords;

    public MsgBodyFilter(String[] keywords) {
        this.keywords = keywords;
    }

    @Override
    public void init() {
    }

    @Override
    public void doFilter(PurchaseMessage purchaseMessage, FilterChain filterChain) {
        String context = purchaseMessage.getContent();
        if (Objects.isNull(context) || context.length() == 0) {
            return;
        }

        //不配置关键词时,过滤器全部放行
        if (Objects.isNull(keywords) || keywords.length == 0) {
            filterChain.doFilter(purchaseMessage);
        } else {
            for (int i = 0; i < keywords.length; i++) {
                //匹配任一关键词,过滤器放行
                if (context.contains(keywords[i])) {
                    filterChain.doFilter(purchaseMessage);
                    break;
                }
            }
        }
    }
}
